package com.endurance.template;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @Author: sunlin
 * @Date: 2024-05-09 8:22
 * @Version: 1.0
 * @Description: pay
 */
@Slf4j
public abstract class AbstractPayCallbackTemplate {

    public String asyncCallback() {
        // 1.支付宝回调参数验证
        HashMap<String, String> verifySignature = verifySignature();

        // 2.参数验证成功，写入到日志集中
        payLog(verifySignature);

        // code = 200，证明解析 各种支付平台的报文成功
        String code = verifySignature.get("code");
        if (!"200".equals(code)) {
            return resultFail();
        }

        // 3. 返回支付结果
        return asyncService(verifySignature);
    }


    protected abstract HashMap<String, String> verifySignature();

    /**
     * 共同行为：打印日志
     *
     * @param verifySignature
     */
    @Async
    public void payLog(HashMap<String, String> verifySignature) {
        log.info("第二步 打印支付输出的日志：{}", verifySignature);
    }

    protected abstract String asyncService(HashMap<String, String> verifySignature);

    protected abstract String resultFail();

    protected abstract String resultSuccess();

}
